package com.scqkxx.gamebox.activity.myself.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.myself.FeedBackActivity;
import com.scqkxx.gamebox.databinding.ItemFeedBackBinding;
import com.scqkzqtz.base.library.utils.ToastUtils;
//import com.scqkzqtz.takepictrue.utils.FileUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zsx on 2018/8/9.
 */

public class FeedBackAdapter extends RecyclerView.Adapter<FeedBackAdapter.ViewHolder> {
    private Context context;
    private List<String> imagePathList = new ArrayList<>();
    private OnClicktItemListener onClickDelectItem;

    public FeedBackAdapter(Context context, List<String> imagePathList) {
        this.context = context;
        this.imagePathList = imagePathList;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return imagePathList.size();
    }

    @Override
    public FeedBackAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_feed_back, parent, false);
        return new FeedBackAdapter.ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(FeedBackAdapter.ViewHolder holder, int position) {
        ItemFeedBackBinding binding = (ItemFeedBackBinding) holder.getBinding();
        String imagePath = imagePathList.get(position);

        if (FeedBackActivity.imageCamera.equals(imagePath)) {
            binding.im.setImageResource(R.mipmap.icon_feed_back_camera);
            binding.imDelect.setVisibility(View.GONE);
        } else {
//            Glide.with(context).load(FileUtil.convertToBitmap(imagePath, 200, 200)).into(binding.im);
            binding.imDelect.setVisibility(View.VISIBLE);
        }

        binding.imDelect.setOnClickListener(v -> {
            imagePathList.remove(imagePath);
            imagePathList.remove(FeedBackActivity.imageCamera);
            imagePathList.add(FeedBackActivity.imageCamera);
            notifyDataSetChanged();
            if (onClickDelectItem != null) {
                onClickDelectItem.onDelectItem(position, imagePathList);
            }
        });

        binding.im.setOnClickListener(v -> {
            if (FeedBackActivity.imageCamera.equals(imagePath) && onClickDelectItem != null) {
                if (imagePathList.size() == FeedBackActivity.MAX_IMAGE_NUMBER + 1) {
                    ToastUtils.showShortToast(context, "上传图片最多" + FeedBackActivity.MAX_IMAGE_NUMBER + "张");
                } else {
                    onClickDelectItem.onClickAddItem();
                }
            }
        });
    }

    public void setImagePathList(List<String> imagePathList) {
        this.imagePathList = imagePathList;
        notifyDataSetChanged();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

        public ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        public ViewDataBinding getBinding() {
            return this.binding;
        }
    }

    public interface OnClicktItemListener {
        void onDelectItem(int position, List<String> imagePathList);

        void onClickAddItem();
    }

    public OnClicktItemListener getOnClickDelectItem() {
        return onClickDelectItem;
    }

    public void setOnClickDelectItem(OnClicktItemListener onClickDelectItem) {
        this.onClickDelectItem = onClickDelectItem;
    }
}